package com.feiyuan.core.service.impl;

import com.feiyuan.common.exception.BusinessException;
import com.feiyuan.core.mapper.AdminMenuMapper;
import com.feiyuan.core.pojo.entity.AdminMenu;
import com.feiyuan.core.pojo.entity.AdminRoleMenu;
import com.feiyuan.core.mapper.AdminRoleMenuMapper;
import com.feiyuan.core.service.AdminRoleMenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Xupf
 * @since 2022-09-06
 */
@Service
public class AdminRoleMenuServiceImpl extends ServiceImpl<AdminRoleMenuMapper, AdminRoleMenu> implements AdminRoleMenuService {

    @Autowired
    private AdminMenuMapper adminMenuMapper;

    @Override
    public List<AdminMenu> selectMenus() {
        List<AdminMenu> menus = adminMenuMapper.selectList(null);
        List<AdminMenu> adminMenus = menus.stream().filter(item -> {
            return item.getParentId() == 0;
        }).map(menu -> {
            menu.setChildren(this.getChildren(menu, menus));
            return menu;
        }).sorted((menu1, menu2) -> {
            return menu1.getMenuSort() - menu2.getMenuSort();
        }).collect(Collectors.toList());
        return adminMenus;
    }

    @Override
    public void saveOrUpdateMenus(AdminRoleMenu adminRoleMenu) {
        List<Long> menuIds = adminRoleMenu.getMenuIds();
        if (menuIds == null || menuIds.size() <= 0) {
            throw new BusinessException("所选的菜单集合不能为空", -101);
        }
        // 先删除
        this.baseMapper.delete(new QueryWrapper<AdminRoleMenu>().eq("role_id", adminRoleMenu.getRoleId()));
        // 在保存
        this.saveRoleMenu(adminRoleMenu, menuIds);
    }

    @Override
    public List<Long> getByRoleId(Long roleId) {
        List<AdminRoleMenu> roleMenus = this.baseMapper.selectList(new QueryWrapper<AdminRoleMenu>().eq("role_id", roleId));
        List<Long> list = new ArrayList<>();
        if (roleMenus != null && roleMenus.size() > 0) {
            List<Long> menuIds = roleMenus.stream().map(AdminRoleMenu::getMenuId).collect(Collectors.toList());
            list.addAll(menuIds);
        }
        return list;
    }

    @Override
    public List<String> selectUserMenus(Long userId) {
        List<String> perms = this.baseMapper.selectUserMenus(userId);
        List<String> strs = new ArrayList<>();
        if (perms != null && perms.size() > 0) {
            for (String perm : perms) {
                if (StringUtils.isNotEmpty(perm)) {
                    String[] split = perm.split(",");
                    for (String str : split) {
                        strs.add(str);
                    }
                }
            }
        }
        if (strs == null || strs.size() <= 0) {
            strs.add("admin_user");
        }
        return strs;
    }

    /**
     * 保存角色与菜单
     * @param adminRoleMenu
     * @param menuIds
     */
    private void saveRoleMenu(AdminRoleMenu adminRoleMenu, List<Long> menuIds) {
        for (Long menuId : menuIds) {
            AdminRoleMenu roleMenu = new AdminRoleMenu();
            roleMenu.setRoleId(adminRoleMenu.getRoleId());
            roleMenu.setMenuId(menuId);
            this.baseMapper.insert(roleMenu);
        }
    }

    /**
     * 查询子菜单
     * @param root
     * @param all
     * @return
     */
    private List<AdminMenu> getChildren(AdminMenu root, List<AdminMenu> all) {
        List<AdminMenu> menus = all.stream().filter(item -> {
            return item.getParentId() == root.getId();
        }).map(menu -> {
            menu.setChildren(this.getChildren(menu, all));
            return menu;
        }).sorted((menu1, menu2) -> {
            return menu1.getMenuSort() - menu2.getMenuSort();
        }).collect(Collectors.toList());
        return menus;
    }
}
